<?php

namespace App\Morefun\Quiz;

use App\Contracts\Repositories\QuizRepository as QuizRepositoryContact;
use App\Entities\Quiz;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\DB;

class QuizRepository implements QuizRepositoryContact
{
    public function create(array $data)
    {
        $quiz = new Quiz();
        $data = array_merge(['configurations' => [], 'validations' => []], $data);
        $quiz->fill($data);

        $quiz->save();

        return $quiz->id;
    }

    public function find($id, $columns = ['*'])
    {
        return Quiz::withOptions()->where('id', $id)->first($columns);
    }

    public function findAll(array $conditions, $columns = ['*'])
    {
        return Quiz::query()
            ->where($conditions)
            ->orderBy('created_at', 'ASC')
            ->get($columns);
    }

    public function types()
    {
        return [
            Quiz::TYPE_CHOICE_SINGLE => '单选',
            Quiz::TYPE_CHOICE_MULTI  => '多选',
            Quiz::TYPE_FILL_BLANK    => '填空',
            // Quiz::TYPE_MARK_SCORE => '评分题',
            // Quiz::TYPE_DROP_DOWN => '下拉框',
        ];
    }

    public function update($id, array $data)
    {
        $quiz = Quiz::query()->where('id', $id)->first();

        if (null === $quiz) {
            return false;
        }

        $data['configurations'] = [
            'linked'          => Arr::get($data, 'linked', false),
            'link_id'         => Arr::get($data, 'links.id'),
            'link_option_ids' => Arr::get($data, 'links.option_ids', []),
        ];

        $data['validations'] = [
            'required' => Arr::get($data, 'required', false),
        ];

        $quiz->fill($data);

        return $quiz->save();
    }

    public function delete($id)
    {
        return Quiz::query()->where('id', $id)->delete();
    }

    public function setSort($id, $sort)
    {
        $quiz = Quiz::query()->where('id', $id)->first();

        if (null === $quiz) {
            return false;
        }

        $quiz->sort = $sort + 0;

        return $quiz->save();
    }

    public function paginateWithOptions($activityId, $perPage, $columns = ['*'])
    {
        return Quiz::withOptions()
            ->where('activity_id', $activityId)
            ->simplePaginate($perPage, $columns);
    }

    public function findAllWithOptions($activityId, array $columns = ['*'], $withoutType = null)
    {
        $query = Quiz::withOptions()->where('activity_id', '=', $activityId);

        if ($withoutType !== null) {
            $query = $query->where('type', '!=', Quiz::TYPE_FILL_BLANK);
        }

        return $query->orderBy('created_at', 'ASC')->get($columns);
    }

    public function findIn($column, array $values, $withOptions = false, array $columns = ['*'])
    {
        if ($withOptions) {
            $query = Quiz::withOptions();
        } else {
            $query = Quiz::query();
        }

        return $query->whereIn($column, $values)->orderBy('created_at', 'ASC')->get($columns);
    }

    public function findQuizResult($activityId, $page = 1, $limit = 0)
    {
        $start = ($page - 1) * $limit;

        $query = DB::table('quiz_papers')
            ->leftJoin('participants', 'quiz_papers.uid', '=', 'participants.uniqueid')
            ->leftJoin('quiz_sheets', 'quiz_papers.id', '=', 'quiz_sheets.paper_id')
            ->leftJoin('quizzes', 'quiz_sheets.quiz_id', '=', 'quizzes.id')
            ->where('quiz_papers.activity_id', $activityId)
            ->whereNull('quizzes.deleted_at')
            ->orderBy('quiz_papers.created_at', 'ASC')
            ->select('quizzes.id','quiz_sheets.activity_id','participants.nickname','participants.mobile', 'participants.openid','quizzes.title', 'quizzes.type', 'quiz_sheets.fill_content','quiz_sheets.option_id','quiz_papers.created_at');
        if ($limit > 0) {
            $query->limit($limit)->skip($start);
        }

        return $query->get();
    }
}
